package com.shanghai.market.plshnaghai.net;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.shanghai.market.plshnaghai.AppApplication;
import com.shanghai.market.plshnaghai.entity.AppData;
import com.shanghai.market.plshnaghai.publics.LogCat;

import org.json.JSONObject;

import java.util.Map;


/**
 * 业务基类
 */
final public class HttpManager {
    private static final int VOLLEY_TIMEOUT = 50 * 1000;

    private static class HttpHodler {
        private static HttpManager instance = new HttpManager();
    }

    public static HttpManager getInstance() {
        return HttpHodler.instance;
    }

    private HttpManager() {
        super();
    }

    public void executePost(int method, String url, final VolleyParams params, Listener<JSONObject> listener,
                            ErrorListener error) {

        LogCat.i("url:" + url);

        JSONObject paramJsonObject = new JSONObject(params);
        LogCat.i("params:" + paramJsonObject);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(method, url, paramJsonObject, listener, error) {


            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                VolleyParams headers = AppApplication.getInstance().initHttpHeader();
                LogCat.i("headers:" + headers);
                return headers;
            }

            @Override
            protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
                return super.parseNetworkResponse(response);
            }
        };
        jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(VOLLEY_TIMEOUT, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        RequestManager.addRequest(jsonObjectRequest, this);
    }

    public void executeGet(int method, String url, Listener<String> listener,
                           ErrorListener error) {

        LogCat.i("url:" + url);

        final StringRequest request = new StringRequest(method, url, listener, error) {


            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String cookie = response.headers.get("Set-Cookie");
                if (null != cookie) {
                    AppData data = AppApplication.getInstance().getAppData();
                    data.setCookie(cookie);
                    AppApplication.getInstance().saveAppData(data);
                    data = null;
                }
                return super.parseNetworkResponse(response);
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                VolleyParams headers = AppApplication.getInstance().initHttpHeader();
                LogCat.i("headers:" + headers);
                return headers;
            }
        };
        request.setRetryPolicy(new DefaultRetryPolicy(VOLLEY_TIMEOUT, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        RequestManager.addRequest(request, this);
    }

    public void cancelAll() {
        RequestManager.cancelAll(this);
    }

    public void get(String url, Listener<String> listener, ErrorListener error) {
        AppApplication app = AppApplication.getInstance();
        executeGet(Request.Method.GET, url + app.sig(), listener, error);
    }

    //    public void post(String url, VolleyParams params, Listener<String> listener, ErrorListener error) {
//        AppApplication app = AppApplication.getInstance();
//        execute(Method.POST, url + app.sig(), params, listener, error);
//    }
    public void post(String url, VolleyParams params, Listener<JSONObject> listener, ErrorListener error) {
        AppApplication app = AppApplication.getInstance();
        executePost(Request.Method.POST, url + app.sig(), params, listener, error);
    }
}
